package com.sais.manage.boot.platform.utils;

import com.sais.manage.boot.platform.dto.UserLoginDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Shiro工具类
 *
 * @author zwp
 */
public class ShiroUtils {
    private static Logger logger = LoggerFactory.getLogger(ShiroUtils.class);

    private final SessionDAO sessionDAO;

    public ShiroUtils(SessionDAO sessionDAO) {
        this.sessionDAO = sessionDAO;
    }

    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static void setSessionAttribute(Object key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static Object getSessionAttribute(Object key) {
        return getSession().getAttribute(key);
    }

    public static boolean isLogin() {
        return SecurityUtils.getSubject().getPrincipal() != null;
    }

    public static void logout() {
        SecurityUtils.getSubject().logout();
    }


    public static UserLoginDTO getLoginUser() {
        UserLoginDTO userLoginDTO = (UserLoginDTO) SecurityUtils.getSubject().getPrincipal();
        return userLoginDTO;
    }

}
